﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DeviceDebugger.Model;
using DeviceDebugger.DataAccess;
using System.IO;
using System.Windows.Controls;
using System.Windows;
using GalaSoft.MvvmLight.Messaging;

namespace DeviceDebugger.ViewModel
{
    public class SettingWindowsViewModel : ViewModelBase
    {
        private StackerModel _model;
        public StackerModel Model
        {
            get { return _model; }
            set { _model = value; RaisePropertyChanged(() => Model); }
        }

        private string rowCount;
        private string columCount;
        private string layerCount;
        private string lanewayNo;
        public string LanewayNo
        {
            get { return lanewayNo; }
            set { lanewayNo = value; RaisePropertyChanged(() => LanewayNo); }
        }


        public string LayerCount
        {
            get { return layerCount; }
            set { layerCount = value; RaisePropertyChanged(() => LayerCount); }
        }
        public string ColumCount
        {
            get { return columCount; }
            set { columCount = value; RaisePropertyChanged(() => ColumCount); }
        }
        public string RowCount
        {
            get { return rowCount; }
            set { rowCount = value; RaisePropertyChanged(() => RowCount); }
        }

        private Button saveButton;
        public Button SaveButton
        {
            get { return saveButton; }
            set { saveButton = value; RaisePropertyChanged(() => SaveButton); }
        }
        /// <summary>
        /// 当前页面是否是添加动作
        /// </summary>
        private bool IsAdd { get; set; } = true;

        public SettingWindowsViewModel()
        {
            SaveButton = new();
            SaveButton.Content = "保存并创建任务";
            SaveButton.IsEnabled = true;

            Model = new();

            Messenger.Default.Register<StackerModel>(this, "Settings", AddorEditAction);
        }

        private void AddorEditAction(StackerModel obj)
        {
            IsAdd = false;
            Model = obj;
            LanewayNo = Model.LanewayNo.ToString();
            RowCount = Model.RowCount.ToString();
            ColumCount = Model.ColumCount.ToString();
            LayerCount = Model.LayerCount.ToString();
        }

        /// <summary>
        /// 保存按钮点击命令
        /// </summary>
        public RelayCommand<Window> SaveCommand => new RelayCommand<Window>(SaveCommand_Click);
        /// <summary>
        /// 关闭按钮点击命令
        /// </summary>
        public RelayCommand<Window> CloseCommand => new RelayCommand<Window>(CloseCommand_Click);

        private void CloseCommand_Click(Window obj)
        {
            CloseWindow(obj);
        }

        private void CloseWindow(Window win)
        {
            //初始化页面控件显示
            Model = new();
            RowCount = string.Empty;
            ColumCount = string.Empty;
            LayerCount = string.Empty;
            LanewayNo = string.Empty;
            SaveButton = new();
            IsAdd = true;
            SaveButton.Content = "保存并创建任务";
            SaveButton.IsEnabled = true;
            //Messenger.Default.Unregister(this);
            win.Close();
        }

        private void SaveCommand_Click(Window window)
        {
            if (string.IsNullOrWhiteSpace(RowCount) || string.IsNullOrWhiteSpace(ColumCount) || string.IsNullOrWhiteSpace(LayerCount))
                return;

            bool bResult = false;

            SaveButton.Content = "正在创建作业...";
            SaveButton.IsEnabled = false;
            Model.LanewayNo = int.Parse(LanewayNo);
            Model.RowCount = int.Parse(RowCount);
            Model.ColumCount = int.Parse(ColumCount);
            Model.LayerCount = int.Parse(LayerCount);

            if (IsAdd)
                bResult = DataVisitor.AddStacker(Model) > 0;
            else
                bResult = DataVisitor.EditStacker(Model);
            if (bResult)
            {
                Messenger.Default.Send("", "SettingsReturn");

                CloseWindow(window);
            }

        }





    }
}
